package dev.touchswipe.ScreenLock
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.utils.setTimeout;
	
	[Event(name="complete",type="flash.events.Event")]
	
	public class PinInputPanel extends Sprite
	{
		private var _pin:String="";
		public function get pin():String{
			return _pin;
		}
		public function PinInputPanel()
		{
			draw();
			for(var i:int=0; i<3; i++){
				for(var j:int=0; j<3; j++){
					var pinBtn:PinButton=new PinButton(3*j+i+1);
					pinBtn.x=20+(pinBtn.width+2)*i;
					pinBtn.y=90+(pinBtn.height+2)*j;
					pinBtn.addEventListener(MouseEvent.CLICK,_enterKey);
					addChild(pinBtn);
				}
			}
			var pinBtn0:PinButton=new PinButton(0);
			pinBtn0.x=20+(pinBtn.width+2);
			pinBtn0.y=90+(pinBtn.height+2)*3;
			pinBtn0.addEventListener(MouseEvent.CLICK,_enterKey);
			addChild(pinBtn0);
		}
		
		private function draw():void{
			graphics.clear();
			graphics.beginFill(0x000000,0.6);
			graphics.drawRoundRect(0,0,344,396,90);
			graphics.beginFill(0xFFFFFF);
			graphics.drawRoundRect(344/2-100,20,200,50,50);
			graphics.beginFill(0);
			for (var i:int=0; i<_pin.length; i++){
				graphics.drawCircle(344/2-75+(i)*50,45,15);
			}
		}
		
		private function _enterKey(event:MouseEvent):void{
			if(_pin.length<4){
				_pin+=PinButton(event.currentTarget).number.toString();
				draw();
				if(_pin.length==4){
					var evt:Event=new Event(Event.COMPLETE,false,true);
					dispatchEvent(evt);
					if(evt.isDefaultPrevented()){
						setTimeout(reset,1000);
					}
				}
			}
		}
		
		public function reset():void{
			_pin="";
			draw();
		}
	}
}